const db = require('../../dataBase/index')

const permissionHandle = {
  // 获取权限列表
  getPermissionInfo(req, res) {
    const queryInfo = req.params.type  // list
    sqlSelectStr = `SELECT * FROM permission`
    db.query(sqlSelectStr, (err, results) => {
      if (err) return res.cwcl('数据库查询语句执行失败！' + err)
      if (queryInfo === 'list') {
        res.send({
          meta: {
            code: 200,
            msg: '权限数据列表获取成功！'
          },
          data: results
        })
      }
      if (queryInfo === 'tree') {
        qxsList = []
        results.forEach((item, index) => {
          item.children = []
          let i = parseInt((item.childrenpd % 100 / 10)) - 1

          if (/^1/.test(item.childrenpd)) {  // 第一层
            qxsList.push(item)
          }
          if (/^2/.test(item.childrenpd)) {
            qxsList[i].children.push(item)
          }
          if (/^3/.test(item.childrenpd)) {
            delete item.children
            let j = item.childrenpd % 10
            qxsList[i].children[j].children.push(item)
          }
        })
        res.send({
          meta: {
            code: 200,
            msg: '权限数据树获取成功'
          },
          data: qxsList
        })
      }
    })
  },
  // 获取角色列表
  getRolesList(req, res) {
    sqlSelectStr = 'SELECT * FROM roles'
    db.query(sqlSelectStr, (err, results) => {
      if (err) return res.cwcl('数据库查询语句执行失败！')
      const jgcl = []
      results.forEach(element => {
        element.children = []


        if (/^1/.test(element.childrenpd)) {  // 第一层
          jgcl.push(element)
        }
        if (/^2/.test(element.childrenpd)) {  // 第二层
          let i = parseInt(element.childrenpd % 100 / 10) - 1
          jgcl[i].children.push(element)
        }
        if (/^3/.test(element.childrenpd)) {  // 第三层
          let i = parseInt(element.childrenpd % 100 / 10) - 1
          let j = element.childrenpd % 10
          jgcl[i].children[j].children.push(element)
        }
        let k = parseInt(element.childrenpd % 1000 % 100 / 10)
        if (/^4/.test(element.childrenpd)) {  // 第四层
          delete element.children
          let j = element.childrenpd % 10
          let i = parseInt(element.childrenpd % 1000 / 100) - 1
          if (jgcl[i].children[k] !== undefined) {
            if (jgcl[i].children[k].children[j] !== undefined) {
              jgcl[i].children[k].children[j].children.push(element);
            }
          }
        }

      });
      res.send({
        meta: {
          code: 200,
          msg: '角色列表请求成功！'
        },
        data: jgcl
      })
    })
  },
  // 根据 id 删除角色的权限
  deleteRoles(req, res) {
    const queryInfo = req.params
    const sqlSelectStr = `SELECT * FROM roles WHERE id = ${queryInfo.id}`
    db.query(sqlSelectStr, (err, results) => {
      if (err) return res.cwcl('数据库查询语句执行失败！' + err)

      res.send({
        meta: {
          code: 200,
          msg: '角色权限删除成功！'
        },
        data: results
      })
    })
  }
}

module.exports = permissionHandle
